/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.hogent.designpatterns.billenkar.commands;

import be.hogent.designpatterns.billenkar.Billenkar;
import be.hogent.designpatterns.billenkar.BillenkarVerzameling;
import be.hogent.designpatterns.billenkar.decorators.MetBluts;
import be.hogent.designpatterns.billenkar.decorators.MetKrassen;
import be.hogent.designpatterns.billenkar.decorators.MetPlatteBand;
import be.hogent.designpatterns.billenkar.decorators.MetRoest;
import be.hogent.designpatterns.billenkar.iterator.Iterator;
import be.hogent.designpatterns.billenkar.states.VerhuurdStatus;
import java.util.Scanner;

/**
 *
 * @author Jeroen
 */
public class KiesBillenkarCommand implements Command {

   private BillenkarVerzameling billenkarVerzameling;
   private Scanner in = new Scanner(System.in);
   private Billenkar gekozenBillenkar;
   private Command[] keuzeCommands;


   public KiesBillenkarCommand(BillenkarVerzameling billenkarVerzameling) {
      this.billenkarVerzameling = billenkarVerzameling;
      keuzeCommands = new Command[2];
      //verdere init keuzeCommands na keuze billenkar
   }

   public void execute() {
      int keuze = doeBasisMenu();
      while (keuze == -1) {
         System.out.println("ONGELDIGE KEUZE!");
         keuze = doeBasisMenu();
      }
      Iterator it = billenkarVerzameling.createIterator();
      int i = 0;
      while (it.hasNext() && i < keuze) {
          it.next();
          i++;
      }
      gekozenBillenkar = (Billenkar) it.next();

      keuzeCommands[0] = new VeranderStatusCommand(gekozenBillenkar);
      keuzeCommands[1] = new KiesMankementenCommand(gekozenBillenkar,billenkarVerzameling, i);

      keuze = maakKeuzeMenu();
      while(keuze == -1) {
          System.out.println("ONGELDIGE KEUZE!");
          keuze = maakKeuzeMenu();
      }
      keuzeCommands[keuze-1].execute(); 
      (new HoofdMenuCommand(billenkarVerzameling)).execute();
   }

   private int maakKeuzeMenu() {
      System.out.println("****************************************************");
      System.out.println("                   MAAK KEUZE");
      System.out.println("****************************************************");
      System.out.println("");
      System.out.println("");
      System.out.println("1. Status aanpassen");
      System.out.println("2. Mankement toevoegen");
      String inv = in.nextLine();
      int keuze;
      try {
         keuze = Integer.parseInt(inv);
         if(keuze<1 || keuze>2)
             throw new NumberFormatException();
      } catch (NumberFormatException nfe) {
         keuze = -1;
      }
      return keuze;
   }


   private int doeBasisMenu() {
      System.out.println("****************************************************");
      System.out.println("                   KIES BILLENKAR");
      System.out.println("****************************************************");
      System.out.println("");
      System.out.println("");
      System.out.println("-----------------KIES BILLENKAR-------------------");
      Iterator it = billenkarVerzameling.createIterator();
      int i = 0;
      while (it.hasNext()) {
         Billenkar b = (Billenkar)it.next();
         System.out.println(i + ". " + b.getOmschrijving()+"    Prijs: "+b.getPrijs()+"  Status: "+b.getStatus().toString());
         i++;
      }
      String inv = in.nextLine();
      int keuze;
      try {
         keuze = Integer.parseInt(inv);
         if(keuze<0 || keuze>=i)
             throw new NumberFormatException();
      } catch (NumberFormatException e) {
         keuze = -1;
      }
      return keuze;
   }

}
